﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YLW.Plat.Jhfw.HL7V3.XSDModels.PRPM_IN406110UV01;
using YLW.Plat.Jhfw.Model.JhfwKsgl;

namespace YLW.Plat.Jhfw.HL7V3.V3Message
{
    public class V3_PRPM_IN406110UV01
    {
        public static string OrganizationInfoQuery_Response(GetJhfwKsglListOut jsonData, string id_extension, string sender_name, string receiver_name)
        {
            string hl7Xml = string.Empty;

            if (jsonData == null)
            {
                return hl7Xml;
            }

            //根据XML XSD构建HL7 XML消息格式

            //创建根对象
            var xmlRoot = new PRPM_IN406110UV01
            {
                ITSVersion = "XML_1.0",
                id1 = new id1 { root = "2.16.156.10011.2.5.1.1", extension = Guid.NewGuid().ToString() },
                creationTime = new creationTime { value = DateTime.Now.ToString("yyyyMMddHHmmss") },
                interactionId = new interactionId { root = "2.16.156.10011.2.5.1.2", extension = "PRPM_IN406110UV01" },
                processingCode = new processingCode { code = "P" },
                processingModeCode = new processingModeCode(),
                acceptAckCode = new acceptAckCode { code = "AL" },
                receiver = new receiver
                {
                    typeCode = "RCV",
                    device1 = new device1
                    {
                        classCode = "DEV",
                        determinerCode = "INSTANCE",
                        id1 = new id1
                        {
                            item = new item
                            {
                                root = "2.16.156.10011.2.5.1.3",
                                extension = receiver_name
                            }
                        }
                    }
                },
                sender = new sender
                {
                    typeCode = "SND",
                    device1 = new device1
                    {
                        classCode = "DEV",
                        determinerCode = "INSTANCE",
                        id1 = new id1
                        {
                            item = new item
                            {
                                root = "2.16.156.10011.2.5.1.3",
                                extension = sender_name
                            }
                        }
                    }
                },
                acknowledgement = new acknowledgement
                {
                    typeCode = jsonData.subcode == "success" ? "AA" : "AE",
                    targetMessage = new id
                    {
                        id1 = new id1
                        {
                            root = "2.16.156.10011.2.5.1.1",
                            extension = id_extension
                        }
                    },
                    acknowledgementDetail = new acknowledgementDetail
                    {
                        text = new text
                        {
                            value = jsonData.submsg
                        }
                    }
                },
                controlActProcess = new controlActProcess
                {
                    classCode = "CACT",
                    moodCode = "EVN",

                    queryAck = new queryAck
                    {
                        queryResponseCode = new queryResponseCode { code = jsonData.subcode == "success" ? "OK" : "ERR-1" },
                    }
                },
            };

            List<subject> subList = new List<subject>();

            if (jsonData != null && jsonData.data != null)
            {
                foreach (var listItem in jsonData.data)
                {
                    subList.Add(new subject
                    {
                        typeCode = "SUBJ",
                        registrationEvent = new registrationEvent
                        {
                            classCode = "REG",
                            moodCode = "EVN",
                            statusCode = new statusCode { code = "active" },
                            subject1 = new subject1
                            {
                                typeCode = "SBJ",
                                assignedEntity1 = new assignedEntity1
                                {
                                    classCode = "ASSIGNED",
                                    id1 = new id1
                                    {
                                        item = new item { root = "2.16.156.10011.1.26", extension = listItem.deptid }
                                    },
                                    Items = new object[] {
                                        new code
                                        {
                                            //code1 = "A03.01",
                                            codeSystem = "2.16.156.10011.2.3.2.62",
                                            codeSystemName = "医疗卫生机构业务科室分类与代码表",
                                            //displayName=new displayName{ value="呼吸内科专业" }
                                        },
                                        new name
                                        {
                                            item= new item
                                            {
                                                part=new part
                                                {
                                                    value=listItem.deptname
                                                }
                                            }
                                        },
                                        new addr
                                        {
                                            item=new item
                                            {
                                                part=new part
                                                {
                                                    value=listItem.restrictrdn
                                                }
                                            }
                                        },
                                        new telecom
                                        {
                                            item=new item{
                                                value=listItem.depttelephone
                                            }
                                        },
                                        new statusCode
                                        {
                                            code="active"
                                        },
                                        new assignedPrincipalOrganization
                                        {
                                            classCode="ORG",
                                            determinerCode="INSTANCE",
                                            id1=new id1
                                            {
                                                item=new item
                                                {
                                                    root="2.16.156.10011.1.26",
                                                    extension=listItem.forward
                                                }
                                            },
                                            name=new name
                                            {
                                            }
                                        }
                                    }
                                }
                            },
                            custodian = new custodian
                            {
                                typeCode = "CST",
                                assignedEntity1 = new assignedEntity1
                                {
                                    classCode = "ASSIGNED",
                                    id1 = new id1
                                    {
                                        item = new item
                                        {
                                            root = "2.16.156.10011.1.4",
                                            extension = listItem.in_empid
                                        }
                                    },
                                    Items = new object[] {
                                        new assignedPerson
                                        {
                                            classCode="PSN",
                                            determinerCode="INSTANCE",
                                            name=new name
                                            {
                                            }
                                        },
                                        new representedOrganization
                                        {
                                            classCode="ORG",
                                            determinerCode="INSTANCE",
                                            id1=new id1
                                            {
                                                item=new item
                                                {
                                                    root="2.16.156.10011.1.26"
                                                }
                                            },
                                            name=new name
                                            {
                                            },
                                            contactParty=new contactParty
                                            {
                                                classCode="CON",
                                                contactPerson=new contactPerson{
                                                    classCode="PSN",
                                                    determinerCode="INSTANCE",
                                                    name=new name
                                                    {
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            },
                            extension = new extension
                            {
                                sjly = new value { value1 = listItem.sjly },
                                lyxt = new value { value1 = listItem.lyxt },
                                hoscode = new value { value1 = listItem.hoscode },
                                deptid = new value { value1 = listItem.deptid },
                                deptname = new value { value1 = listItem.deptname },
                                bm = new value { value1 = listItem.bm },
                                py = new value { value1 = listItem.py },
                                stay = new value { value1 = listItem.stay },
                                deptfun = new value { value1 = listItem.deptfun },
                                onesubject = new value { value1 = listItem.onesubject },
                                secondsubject = new value { value1 = listItem.secondsubject },
                                sort = new value { value1 = listItem.sort },
                                restrictrdn = new value { value1 = listItem.restrictrdn },
                                lxr = new value { value1 = listItem.lxr },
                                depttelephone = new value { value1 = listItem.depttelephone },
                                forward = new value { value1 = listItem.forward },
                                kssj = new value { value1 = listItem.kssj },
                                jssj = new value { value1 = listItem.jssj },
                                useflag = new value { value1 = listItem.useflag },
                                in_empid = new value { value1 = listItem.in_empid },
                                in_date = new value { value1 = listItem.in_date },
                                xg_empid = new value { value1 = listItem.xg_empid },
                                xgsj = new value { value1 = listItem.xgsj }
                            }
                        }
                    });
                }
            }

            xmlRoot.controlActProcess.subject = subList.ToArray();

            hl7Xml = HL7V3Helper.SerializeToXml(xmlRoot);

            return hl7Xml;
        }
    }
}
